﻿using System;
using eCommerce.core;
using eCommerce.core.AdminUser;
using eCommerce.core.Authenticated;
using eCommerce.core.Helpers;

namespace eCommerce.cms.Controls.AdminUser
{
    public partial class CreateOrEdit : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {                           
            if (!IsPostBack)
            {                
                ltTitle.Text = "Tạo mới thành viên";

                if (Request.QueryString[Utils   .UserId] != null)
                {
                    var adminUserId = Utils.GetIdFromRequestString(Utils.UserId, 0);

                    if (adminUserId > 0)
                    {
                        ltTitle.Text            = "Sửa thông tin thành viên";
                        panelPassword.Visible   = false;
                        txtUserName.ReadOnly    = true;

                        BindUser(adminUserId);
                    }
                }
            }
        }

        private void BindUser(int id)
        {
            var o = AdminUserController.AdminUser_GetById(id);
            if (o != null)
            {
                ddlStore.SelectedValue  = o.StoreId.ToString();
                txtUserName.Text        = o.Username;
                txtEmail.Text           = o.Email;
                txtFullName.Text        = o.FullName;                
                txtInformation.Text     = o.Information;
                txtBirthday.Text        = Utils.FormatIn_DDMMYYYY(o.Birthday);                                                

                GetGender(o);
                cbStatus.Checked = o.Status == AdminUserObject.IsActived;
            }
        }        

        private void GetGender(AdminUserObject o)
        {
            if (o.Gender)
            {
                rbMale.Checked = true;
                rbFemale.Checked = false;
            }
            else
            {
                rbMale.Checked = false;
                rbFemale.Checked = true;
            }
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            bool isDuplicate = txtPassword.Text.Trim() == txtRepassword.Text.Trim();
            if (isDuplicate)
            {
                AdminUserObject o;
                var id = Utils.GetIdFromRequestString(Utils.UserId, 0);
            
                if (id > 0)
                {
                    o = AdminUserController.AdminUser_GetById(id);
                    o.StoreId       = Utils.ToInt32(ddlStore.SelectedValue);
                    o.AdminUserId   = id;
                    o.Email         = txtEmail.Text;
                    o.FullName      = txtFullName.Text;
                    o.Birthday      = Utils.ToDateTime(txtBirthday.Text);
                    o.Gender        = rbMale.Checked;
                    o.Information   = txtInformation.Text;
                    o.Status        = cbStatus.Checked ? AdminUserObject.IsActived : AdminUserObject.IsSuspended;

                    AdminUserController.AdminUser_Update(o);              
                }            
                else
                {
                    o = new AdminUserObject();
                    o.StoreId       = Utils.ToInt32(ddlStore.SelectedValue);
                    o.Username      = txtUserName.Text;
                    o.Password      = SecurityHelper.MD5Encrypt(txtPassword.Text);
                    o.Email         = txtEmail.Text;
                    o.FullName      = txtFullName.Text;
                    o.Birthday      = Utils.ValidDateTime(txtBirthday.Text);
                    o.Gender        = rbMale.Checked;
                    o.Information   = txtInformation.Text;
                    o.Status        = cbStatus.Checked ? AdminUserObject.IsActived : AdminUserObject.IsSuspended;
                    o.CreatedOn     = DateTime.Now;                    
                    o.ModifiedOn    = new DateTime(1900, 1, 1);
                    o.LastLoggedOn  = new DateTime(1900, 1, 1);

                    AdminUserController.AdminUser_Create(o);
                }

                Response.Redirect(UrlHelper.ListUser);      
            }
            else
                ltMessage.Text = Utils.ShowMessage("Không khớp mật khẩu. Vui lòng nhập lại!", MessageType.Warning);            
        }
    }
}